package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements SensitiveService {

    @Override
    public ResponseResult listPage(SensitiveDto dto) {


        IPage<WmSensitive> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(WmSensitive::getSensitives,dto.getName());
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        List<WmSensitive> list = list(wrapper);
        page=  page(page,wrapper);

     ResponseResult result= new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
     result.setData(page.getRecords());
        return result.ok(list);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
       removeById(id);
        return   ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveSensitive(WmSensitive dto) {
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(dto, wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    @Override
    public ResponseResult updateSensitive(WmSensitive dto) {
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setId(dto.getId());
        wmSensitive.setCreatedTime(new Date());
        wmSensitive.setSensitives(dto.getSensitives());
        updateById(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }
}
